package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.ztgf.sys.models.service.ISysApiLogService;
import com.ztgf.sys.models.bo.SysApiLogSaveBO;
import com.ztgf.sys.models.bo.SysApiLogBO;
import com.ztgf.sys.models.dto.SysApiLogDTO;

/**
 * 接口访问日志表控制器
 *
 * @author zhanghualang
 * @Date 2020-07-02 11:45:35
 */

@RestController
@RequestMapping("/cms/sysApiLog")
@Slf4j
@Api(value = "SysApiLogController",tags = "接口访问日志表接口")
public class SysApiLogController {

    @DubboReference
    private ISysApiLogService sysApiLogService;

    @ApiOperation(value = "添加接口访问日志表",notes = "添加接口访问日志表")
    @PostMapping(value = "/insert")
    public Result insert(SysApiLogSaveBO saveBO) {
        boolean flag = sysApiLogService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存接口访问日志表",notes = "保存接口访问日志表")
    public Result save(SysApiLogSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改接口访问日志表",notes = "修改接口访问日志表")
    @PostMapping("/update")
    public Result update(SysApiLogSaveBO saveBO){
        SysApiLogDTO dto = new SysApiLogDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = sysApiLogService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除接口访问日志表",notes = "删除接口访问日志表")
    @ApiImplicitParam(name = "id",value = "接口访问日志表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = sysApiLogService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除接口访问日志表",notes = "批量删除接口访问日志表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        sysApiLogService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysApiLogDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysApiLogDTO dto = sysApiLogService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "接口访问日志表列表",notes = "接口访问日志表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysApiLogDTO>> queryPageList(SysApiLogBO bo){
        Wrapper<SysApiLogDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SysApiLogDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysApiLogService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
